<?php

namespace App\Http\Controllers;

use App\ServicesNew\ConvertibleCardService;
use App\VO\ConvertibleCardUseRecord\Builder as ConvertibleCardUseRecordVOBuilder;
use App\VO\UserConvertibleCard\Builder as UserConvertibleCardVOBuilder;
use Illuminate\Http\Request;

/**
 * 用户权限卡
 * Class UserConvertibleCardController
 * @package App\Http\Controllers
 */
class UserConvertibleCardController extends BaseController
{
    /**
     * @param ConvertibleCardService $convertibleCardService
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(ConvertibleCardService $convertibleCardService)
    {
        $userId = $this->getUserId();
        $userCardEntityList = $convertibleCardService->getUserCardEntityList($userId);
        $vo = (new UserConvertibleCardVOBuilder())->buildList($userCardEntityList);
        return $this->successJsonResult($vo);
    }

    // /**
    //  *  详情页
    //  * @param ConvertibleCardService $convertibleCardService
    //  * @return \Illuminate\Http\JsonResponse
    //  */
    // public function show(Request $request, ConvertibleCardService $convertibleCardService)
    // {
    //     $cardEntity = $convertibleCardService->getEntityFromRequest($request);
    //     if (! $cardEntity) {
    //         return $this->showJsonResult(201, 'notfound');
    //     }
    //     $vo = (new ConvertibleCardVOBuilder)->buildDetail([
    //         'cardentity' => $cardEntity,
    //     ]);
    //     return $this->showJsonResult(200, 'ok', $vo);
    // }

    /**
     * 使用记录
     * @param Request $request
     * @param ConvertibleCardService $convertibleCardService
     * @return \Illuminate\Http\JsonResponse
     */
    public function records(Request $request, ConvertibleCardService $convertibleCardService)
    {
        $userCardId = (int)$request->input('user_card_id');
        $cardId = (int)$request->input('card_id');
        $cardEntity = $convertibleCardService->getCardEntityById($cardId);
        $userCardEntity = $convertibleCardService->getUserCardEntityByUserCardId($userCardId);
        $recordEntityList = $convertibleCardService->getUserCardRecordEntityList($userCardId);
        $vo = (new ConvertibleCardUseRecordVOBuilder())->buildDetail([
            'recordlist' => $recordEntityList,
            'cardentity' => $cardEntity,
            'usercardentity' => $userCardEntity,
        ]);
        return $this->successJsonResult($vo);
    }
}
